﻿package net.wangit.dev.wizard;

import java.util.HashMap;
import java.util.Map;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.dev.wizard.unit.AUnitGroup;
import net.wangit.framework.validation.ValidationResult;
import net.wangit.framework.validation.ValidationUtil;
import net.wangit.jdbc.DB;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitFactory;


public abstract class OneTableSuiteProcessor {

	protected UnitComponent unit;
	protected String unitId;
	protected String domain;
	protected boolean addGroup;
	protected String tableName;
	protected String groupId;
	protected String groupName;
	protected String page1Id;
	protected String page1Name;
	protected String queryUnitId;
	protected String queryUnitName;
	protected String listUnitId;
	protected String listUnitName;
	protected String formUnitId;
	protected String formUnitName;
	protected DB db;
	protected ActionContext ac;

	public OneTableSuiteProcessor() {
	}

	public void process(DB db, ActionContext ac, String unitId)
			throws Exception {
		init(db, ac, unitId);
		if (!invalid()) {
			ac.getActionResult().setSuccessful(false);
			return;
		}
		if (addGroup)
			AUnitGroup.create(db, groupId, groupName);
		createUnits();
	}

	private void init(DB db, ActionContext ac, String unitId) {
		this.db = db;
		this.ac = ac;
		this.unitId = unitId;
		unit = UnitFactory.getUnitWithoutClone("dev", unitId);
		domain = ConsoleDomainUtil.getDomain(ac);
		tableName = StringUtil
				.clearSpace(ac.getRequestParameter((new StringBuilder(String
						.valueOf(unitId))).append(".tablename").toString()));
		groupId = StringUtil
				.clearSpace(ac.getRequestParameter((new StringBuilder(String
						.valueOf(unitId))).append(".groupid").toString()));
		groupId = toLower(groupId);
		groupName = ac.getRequestParameter((new StringBuilder(String
				.valueOf(unitId))).append(".groupname").toString());
		page1Id = StringUtil
				.clearSpace(ac.getRequestParameter((new StringBuilder(String
						.valueOf(unitId))).append(".page1id").toString()));
		page1Id = toLower(page1Id);
		page1Name = ac.getRequestParameter((new StringBuilder(String
				.valueOf(unitId))).append(".page1name").toString());
		queryUnitId = StringUtil
				.clearSpace(ac.getRequestParameter((new StringBuilder(String
						.valueOf(unitId))).append(".queryunitid").toString()));
		queryUnitId = toLower(queryUnitId);
		queryUnitName = ac.getRequestParameter((new StringBuilder(String
				.valueOf(unitId))).append(".queryunitname").toString());
		listUnitId = StringUtil
				.clearSpace(ac.getRequestParameter((new StringBuilder(String
						.valueOf(unitId))).append(".listunitid").toString()));
		listUnitId = toLower(listUnitId);
		listUnitName = ac.getRequestParameter((new StringBuilder(String
				.valueOf(unitId))).append(".listunitname").toString());
		formUnitId = StringUtil
				.clearSpace(ac.getRequestParameter((new StringBuilder(String
						.valueOf(unitId))).append(".formunitid").toString()));
		formUnitId = toLower(formUnitId);
		formUnitName = ac.getRequestParameter((new StringBuilder(String
				.valueOf(unitId))).append(".formunitname").toString());
		addGroup = "1".equals(ac.getRequestParameter((new StringBuilder(String
				.valueOf(unitId))).append(".addgroup").toString()));
	}

	private String toLower(String id) {
		if (Checker.isEmpty(id))
			return id;
		else
			return id.toLowerCase();
	}

	private boolean invalid() throws Exception {
		ValidationResult vr = new ValidationResult(ac, true);
		vr.setUnitId(unitId);
		WizardUtil.validateTable(db, ac, vr, unit
				.getElementComponent("tablename"), tableName);
		if (addGroup)
			ValidationUtil.isNotNull(vr, ac, groupId, unit
					.getElementComponent("groupid"));
		int count = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String units[][] = getUnits();
		for (int i = 0; i < units.length; i++) {
			String value = units[i][0];
			String id = units[i][1];
			if (!Checker.isEmpty(value)) {
				map.put(value, null);
				count++;
			}
			WizardUtil.validateUnitId(db, ac, vr, unit.getElementComponent(id),
					value);
		}

		if (count != map.size())
			vr.addError(LabelUtil.get(ac, "dev", "wizard.unitid_duplicate"));
		vr.finish();
		return vr.pass();
	}

	protected abstract void createUnits() throws Exception;

	protected abstract String[][] getUnits();
}
